import os
import configparser

config = configparser.ConfigParser()
config.read('../Config/config.ini')

project_path = config.get('ProjectInfo', 'project_path')
project_name = config.get('ProjectInfo', 'project_name')

def get_version():
    print("Checking the existing version of the file...")
    if not os.path.exists(project_path + '/' + project_name + '/Config/Version.ini'):
        print("The version file was not found")
        with open(project_path + '/' + project_name + '/Config/Version.ini', 'w') as f:
            f.write('1.0.0')
            print("The version file has been created")
    with open(project_path + '/' + project_name + '/Config/Version.ini', 'r') as f:
        return f.read().strip()


def write_version(version):
    with open(project_path + '/' + project_name + '/Config/Version.ini', 'w') as f:
        f.write(version)

def UpdateVersion(version_type):
    version = get_version()

    if version_type == '0':
        version_parts = version.split('.')
        version_parts[0] = str(int(version_parts[0]) + 1)
        version_parts[1] = '0'
        version_parts[2] = '0'
        version = '.'.join(version_parts)
    elif version_type == '1':
        version_parts = version.split('.')
        version_parts[1] = str(int(version_parts[1]) + 1)
        version_parts[2] = '0'
        version = '.'.join(version_parts)
    elif version_type == '2':
        version_parts = version.split('.')
        version_parts[2] = str(int(version_parts[2]) + 1)
        version = '.'.join(version_parts)

    print(f"Version file updated!\nVersion: {version}")
    write_version(version)
